﻿$(function() {
    if ($('#readonly').length > 0) {
        $('input,textarea').attr('disabled', true).addClass('disabled');
        $('select').attr('disabled', true).addClass('disabled');
    }
});


function switchTelephoneInput(id) {
    var obj = $('#phone' + id);
    if (obj.val().toString().length == obj.attr('maxlength'))
        $('#phone' + (id + 1)).focus();
    return false;
}


function setTab(id) {
    if (!id) return;
    $('#tab').val(id);
    switch (id) {
        case 'tasks':
            if ($('#tasks table').length == 0) {
                loadTasks();
                break;
            }
        default:
            $('.tab').hide();
            $('#' + id).show();
            break;
    }
    $('#tabs>ul>li>a').removeClass('selected');
    $('#tabs>ul>li>a#a' + id).addClass('selected');
    return false;
}

function loadTasks(reload) {
    if (reload || !$.trim($('#tasks').html()))
        $('#tasks').load(base + 'task.ivp/loadclienttasks/', { tb_clientid: $('#id').val(), timestamp: new Date() }, function() { initTable(); setTab('tasks'); });
    else setTab('tasks');
        return false;
}

function addClient(id) {
    if (id && id > 0) 
    {
        var fio = $('#lastname').val() + ' ' + $('#name').val() + ' ' + $('#patronymicname').val();
        if (self.callback == null) self.callback = opener._clientSelected;
        self.callback(id, fio, 'true');
    }
    self.close();
    return false;
}